package cn.xej.stream;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author alicat.xu
 * @Date 2021/11/22 11:06 下午
 */
public class TestDemo {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3);
        List<Integer> list1 = Arrays.asList(4, 5);

        List<int[]> list2 = list.stream().flatMap(i -> list1.stream().map(j -> new int[]{i, j})).collect(Collectors.toList());


        List<List<AtomicReference<String>>> list3 = list.stream().map(i -> {
            List<AtomicReference<String>> stringList = new ArrayList<>();
            AtomicReference<String> s = new AtomicReference<>("");
            list1.stream().forEach(j -> {
                s.set(i + " " + j);
                stringList.add(s);
            });
            return stringList;
        }).collect(Collectors.toList());

        System.out.println(list3);

//        List<Integer> squares = list.stream().map(x -> x * x).collect(Collectors.toList());
//        System.out.println(squares);

    }

    public static void main1(String[] args) {
        List<Employ> employList = Arrays.asList(new Employ("0001", "张三", 30, "HR", 1),
                new Employ("0002", "李四", 15, "COO", 1), new Employ("0003", "王五", 26, "HR", 1),
                new Employ("0004", "赵六", 34, "COO", 1), new Employ("0005", "李刚", 55, "HR", 1),
                new Employ("0006", "李华", 23, "CTO", 1), new Employ("0007", "王麻子", 30, "HR", 1)
        );


        //forEach方法
//        employList.forEach(Employ::getInfo);


        //limit方法  取前3个
//        employList.stream().limit(3).forEach(Employ::getInfo);


        //skip 与 limit方法，跳过第一个，然后向后取3个
//        employList.stream().skip(1).limit(3).forEach(Employ::getInfo);


        //使用distinct去重
//        List<String> list = employList.stream().map(Employ::getDept).distinct().collect(Collectors.toList());
//        list.forEach(System.out::println);


        //使用Collectors.toSet 无序+去重
//        Set<String> set = employList.stream().map(Employ::getDept).collect(Collectors.toSet());
//        set.forEach(System.out::println);

        //sorted 方法排序
//        employList.stream().sorted(Comparator.comparing(Employ::getAge)).forEach(Employ::getInfo);

        //reversed 方法倒序排序
//        employList.stream().sorted(Comparator.comparing(Employ::getAge).reversed()).forEach(Employ::getInfo);

        //使用map方法以及max方法获取年龄最大的对象年龄
//        Optional<Integer> ageMax = employList.stream().map(Employ::getAge).max((x, y) -> x.compareTo(y));
//        if (ageMax.isPresent()) {
//            System.out.println(ageMax.get());
//        }

        //使用min方法获取年龄最小的对象年龄
//        Optional<Integer> min = employList.stream().map(Employ::getAge).min((x, y) -> x.compareTo(y));
//        if (min.isPresent()){
//            System.out.println(min.get());
//        }

        //使用groupBy分组
//        Map<String, List<Employ>> map = employList.stream().collect(Collectors.groupingBy(Employ::getDept));
//        System.out.println(map);
//
//        map.keySet().forEach(key -> {
//            String name = map.get(key).stream().map(Employ::getName).reduce("|名称: ", (x, y) -> {
//                return x + "," + y;
//            });
//            System.out.println("key "+key+" name "+name);
//        });


        //使用anyMatch来查找
//        boolean match = employList.stream().anyMatch(x -> x.getName().equals("李四"));
//        System.out.println(match);
//
        //用来查找任意一个对象
//        Optional<Employ> any = employList.stream().findAny();
//        if (any.isPresent()){
//            System.out.println(any.get());
//            any.get().getInfo();
//        }

        //使用filter过滤器
        employList.stream().filter(x -> x.getDept().equals("HR")).collect(Collectors.toList()).forEach(Employ::getInfo);

    }

    public static void main3(String[] args) {
        String concat = Stream.of("A","B","C","D").reduce("",String::concat);
        System.out.println(concat);

        concat = Stream.of("a", "B", "c", "D", "e", "F").filter(x -> x.compareTo("Z") > 0).reduce("", String::concat);
        System.out.println(concat);
    }
}
